<?php
session_start();
if (!isset($_SESSION["userid"])) {
    header("Location: /login.html");
    usleep(2 * 1000000);
    exit;
} ?>
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>管理系统</title>
    <style type="text/css">
        body {
            font-family: Arial, sans-serif;
            background-color: #f3f4f6;
            margin: 0;
            padding: 20px;
        }

        .box {
            max-width: 700px;
            margin: 0 auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        h2 {
            text-align: center;
            background-color: aliceblue;
            padding: 10px;
            border-radius: 5px;
            color: #333;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        tr {
            height: 40px;
        }

        th {
            width: 150px;
            text-align: right;
            border: none;
            color: #333;
        }

        td {
            border: none;
        }

        input[type="text"],
        input[type="number"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box;
        }

        .btn {
            background-color: lightskyblue;
            border: none;
            border-radius: 5px;
            padding: 8px 12px;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-right: 10px;
        }

        .btn:hover {
            background-color: deepskyblue;
        }

        .btn-reset {
            background-color: yellowgreen;
        }

        .btn-reset:hover {
            background-color: limegreen;
        }

        .btn-submit {
            background-color: #007bff;
            color: white;
        }

        .btn-submit:hover {
            background-color: #0056b3;
        }

        .radio-group {
            display: flex;
            justify-content: flex-start;
            align-items: center;
        }

        .radio-group label {
            margin-right: 15px;
            font-size: 14px;
        }
    </style>
</head>
<body>
<div class="box">
    <h2>维护学生信息</h2>
    <div class="add">
        <form id="add-form">
            <table border="1">
                <tr>
                    <th>学生编号</th>
                    <td><input type="text" name="id" size="25" required></td>
                </tr>
                <tr>
                    <th>姓　　名</th>
                    <td><input type="text" name="name" size="25" required></td>
                </tr>
                <tr>
                    <th>性　　别</th>
                    <td class="radio-group">
                        <label><input checked type="radio" name="sex" value="男">男</label>
                        <label><input type="radio" name="sex" value="女">女</label>
                    </td>
                </tr>
                <tr>
                    <th>年　　龄</th>
                    <td><input type="number" name="age" size="25" required></td>
                </tr>
                <tr>
                    <th>籍　　贯</th>
                    <td><input type="text" name="city" size="25"></td>
                </tr>
                <tr>
                    <th></th>
                    <td>
                        <input class="btn" type="button" onClick="javascript :history.back(-1);" value="返回">
                        <input class="btn btn-reset" type="reset" value="重置">
                        <input class="btn btn-submit" type="submit" id="add-btn" value="提交">
                    </td>
                </tr>
            </table>
        </form>
    </div>
</div>
<script>

    window.onload = () => {
        const addForm = document.getElementById('add-form');

        addForm.addEventListener('submit', evt => {
            evt.preventDefault();
            const formData = new FormData(addForm);

            const addInfo = {};

            formData.forEach((value, key) => {
                addInfo[key] = value;
            });

            addStudent(addInfo);
            return false;
        });
    };

    // 追加学生信息
    const addStudent = (addInfo) => {
        fetch('dispatch.php?controller=StudentManage&service=addStudent', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(addInfo),

        })
            .then(data => {
                window.location.href = '/';
            })
    }
</script>
</body>
</html>
